package com.edu.test;

import com.edu.bean.City;
import com.edu.bean.Province;
import com.edu.mapper.CityMapper;
import com.edu.mapper.ProvinceMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MyTest2 {

    SqlSession sqlSession = null;

    SqlSession sqlSession2 = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";//路径
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        sqlSession2 = sessionFactory.openSession();
    }


    /*
    *   mybatis 2级缓存：
    *       1 默认开启
    *       2 二级缓存要存放的数据 必须实现 可序列化接口 (implements Serializable)
    *       3 在对应的 mapper.xml中 加上  <cache></cache> 开启这个 mapper的 二级缓存
    *       4 不同的sqlSession 查询数据时 ，二级缓存里的数据是共享的。（二级缓存作用域 是整个 会话工厂）
    *       5 对于查询结果 必须要 (commit 或者 close之后) 才会把数据存放到 二级缓存
     * */
    @Test
    public void test01() throws InterruptedException {

        CityMapper cm1 = sqlSession.getMapper(CityMapper.class);
        CityMapper cm2 = sqlSession2.getMapper(CityMapper.class);
        ProvinceMapper pm1 = sqlSession.getMapper(ProvinceMapper.class);
        ProvinceMapper pm2 = sqlSession.getMapper(ProvinceMapper.class);

        City city1 = cm1.selectCityById(1);//从数据库查 ，city1 放入到一级缓存

        sqlSession.commit();//二级缓存 必须手动提交 才会存入

       Thread.sleep(4999);//线程 睡5秒
        System.out.println("-----");
        City city2= cm2.selectCityById(1); // 从二级缓存获取

        System.out.println(city2);

    }


    /*
    *   二级缓存失效的情况:
    *           1.同一个命名空间下 执行了 增删改 。
    *           2.首当设置 某一个查询 不使用二级缓存
    *           3.其他命名空间的mapper 执行了 增删改 并且手动配置了 关联。
    * */

    @Test
    public void test02(){

        CityMapper cm1 = sqlSession.getMapper(CityMapper.class);
        CityMapper cm2 = sqlSession2.getMapper(CityMapper.class);
        City city = cm1.selectCityById(1); //1一级缓存 session1 里
        sqlSession.commit(); // 1 二级缓存

        cm2.insertCity(new City("阿坝",1));
        sqlSession2.commit(); // 二级缓存失效

        System.out.println("-----");
        City city3 = cm1.selectCityById(1);
        System.out.println(city3);
    }


    @Test
    public void test03(){

        CityMapper cm1 = sqlSession.getMapper(CityMapper.class);
        CityMapper cm2 = sqlSession2.getMapper(CityMapper.class);
        City city = cm1.selectCityById(1); //1一级缓存 session1 里
        sqlSession.commit(); // 1 二级缓存


        System.out.println("-----");
        City city2 = cm2.selectCityById(1);
        System.out.println(city2);
    }


    @Test
    public void test04(){

        CityMapper cm1 = sqlSession.getMapper(CityMapper.class);
        CityMapper cm2 = sqlSession2.getMapper(CityMapper.class);
        ProvinceMapper pm1 = sqlSession.getMapper(ProvinceMapper.class);

        City city = cm1.selectCityById(1); //1一级缓存 session1 里
        sqlSession.commit(); // 1 二级缓存

        pm1.updateProvince(1,"四川省");
        sqlSession.commit();

        System.out.println("-----");
        City city2 = cm2.selectCityById(1);
        System.out.println(city2);
    }
}
